﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XProject.DataVisit.Model.Flow;
using NHibernate;
using NHibernate.Criterion;
using XProject.DataVisit.Model.Temp;

namespace XProject.DataVisit.DAL.Flow
{
    public class DFlowNode:AbstractSupportDal<FlowNode>
    {
        public IList<FlowNode> GetNodeList(Guid id)
        {
            IList<FlowNode> t = new List<FlowNode>();
            Template.Execute(delegate(ISession session)
            {
                t = session.CreateCriteria<FlowNode>()
                    .Add(Expression.Eq("FlowID", id))
                    .AddOrder(Order.Asc("SortOrder"))
                    .List<FlowNode>();
                return t;
            });
            return t;
        }

        public FlowNode SearchPassNode(FlowTemp ft)
        {
            FlowNode t = new FlowNode();
            Template.Execute(delegate(ISession session)
            {
                t = session.CreateCriteria<FlowNode>()
                    .Add(Expression.Eq("FlowID", ft.FlowID))
                    .Add(Expression.Eq("SortOrder", GetById(ft.NodeID).SortOrder+1))
                    .UniqueResult<FlowNode>();
                return t;
            });
            return t;
        }

        public FlowNode SearchRejectNode(FlowTemp ft)
        {
            FlowNode t = new FlowNode();
            Template.Execute(delegate(ISession session)
            {
                t = session.CreateCriteria<FlowNode>()
                    .Add(Expression.Eq("FlowID", ft.FlowID))
                    .Add(Expression.Eq("SortOrder", GetById(ft.NodeID).SortOrder - 1))
                    .UniqueResult<FlowNode>();
                return t;
            });
            return t;
        }

        public FlowNode SearchPassNodeDoc(DocFlowTemp ft)
        {
            FlowNode t = new FlowNode();
            Template.Execute(delegate(ISession session)
            {
                t = session.CreateCriteria<FlowNode>()
                    .Add(Expression.Eq("FlowID", ft.FlowID))
                    .Add(Expression.Eq("SortOrder", GetById(ft.NodeID).SortOrder + 1))
                    .UniqueResult<FlowNode>();
                return t;
            });
            return t;
        }

        public FlowNode SearchRejectNodeDoc(DocFlowTemp ft)
        {
            FlowNode t = new FlowNode();
            Template.Execute(delegate(ISession session)
            {
                t = session.CreateCriteria<FlowNode>()
                    .Add(Expression.Eq("FlowID", ft.FlowID))
                    .Add(Expression.Eq("SortOrder", GetById(ft.NodeID).SortOrder - 1))
                    .UniqueResult<FlowNode>();
                return t;
            });
            return t;
        }
    }
}
